package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.dto.CourseTeacherDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

    @Autowired
    private CourseTeacherMapper courseTeacherMapper;
    @Autowired
    private CourseBaseMapper courseBaseMapper;

    /**
     * 查询教师
     *
     * @param courseId
     * @return
     */
    @Override
    public List<CourseTeacher> getListTeacherByCourseId(Long courseId) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId, courseId);
        return courseTeacherMapper.selectList(queryWrapper);

    }

    /**
     * 添加教师
     *
     * @param courseTeacherDto
     * @param companyid
     * @return
     */
    @Override
    public CourseTeacher addCourseOrSaveTeacher(CourseTeacherDto courseTeacherDto, Long companyid) {
        if (courseTeacherDto == null) {
            XueChengPlusException.cast("参数为空");
        }
        if (companyid == null) {
            XueChengPlusException.cast("机构ID不能为空");
        }
        if (courseTeacherDto.getCourseId() == null) {
            XueChengPlusException.cast("课程id不能为空");
        }
        CourseBase courseBase = courseBaseMapper.selectById(courseTeacherDto.getCourseId());
        if (courseBase==null){
            XueChengPlusException.cast("课程不存在");
        }
        if (!courseBase.getCompanyId().equals(companyid)) {
            XueChengPlusException.cast("只允许在本机构自己的课程中添加老师");
        }

        //先看是否存在
        CourseTeacher courseTeacher = courseTeacherMapper.selectOne(new LambdaQueryWrapper<CourseTeacher>().eq(CourseTeacher::getTeacherName,courseTeacherDto.getTeacherName()));
        if (courseTeacher == null) {
            //新增
            courseTeacher=new CourseTeacher();
            BeanUtils.copyProperties(courseTeacherDto, courseTeacher);
            courseTeacher.setCreateDate(LocalDateTime.now());
            int insert = courseTeacherMapper.insert(courseTeacher);
            if (insert<=0){
                XueChengPlusException.cast("增加老师失败");
            }
            return courseTeacherMapper.selectById(courseTeacher);
        } else {
            //修改
            BeanUtils.copyProperties(courseTeacherDto,courseTeacher);
            int i = courseTeacherMapper.updateById(courseTeacher);
            if (i<=0){
                XueChengPlusException.cast("增加老师失败");
            }
            return courseTeacher;
        }


    }

    /**
     * 删除教师
     * @param courseId
     * @param courseTeacherId
     * @param companyid
     */
    @Override
    public void deleteCourseTeacher(Long courseId, Long courseTeacherId, Long companyid) {
        //进行校验
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        if (courseBase==null){
            XueChengPlusException.cast("课程不存在");
        }
        if (!courseBase.getCompanyId().equals(companyid)){
            XueChengPlusException.cast("不能删除非本机构的教师信息");
        }
        //删除教师信息
        courseTeacherMapper.deleteById(courseTeacherId);

    }


}
